package com.ybear.citypickerview.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;

import com.ybear.citypickerview.R;
import com.ybear.citypickerview.entity.CityEntity;
import com.ybear.citypickerview.holder.CityHolder;
import com.ybear.ybcomponent.base.adapter.BaseViewHolder;
import com.ybear.ybcomponent.base.adapter.BaseXRecyclerViewAdapter;

import java.util.List;

public class CityListAdapter extends BaseXRecyclerViewAdapter<CityEntity> {
    @ColorRes
    private int selectColor = R.color.colorPrimary;
    @ColorRes
    private int unSelectColor = R.color.colorBlack;
    
    public CityListAdapter(@NonNull List<CityEntity> mDataList) {
        super(mDataList);
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CityHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_list_city, parent, false)
        );
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, final int position) {
        super.onBindViewHolder(viewHolder, position);
        CityEntity data = getDataList().get( position );
        CityHolder holder = (CityHolder) viewHolder;
        TextView tvTitle = holder.getTitle();
        tvTitle.setText( data.getName() );

        //设置Item的文本样式
        if( mOnTextStyle != null ) mOnTextStyle.onItem( holder );
        //设置标题颜色
        holder.setTitleColor( data.isSelect() ? selectColor : unSelectColor );
        //设置按下背景色
        holder.setBackgroundResource(
                data.isSelect() ? R.color.colorWhile : R.drawable.selector_def_btn
        );
    }

    @Override
    public int setUpResources() {
        return 0;
    }

    @Override
    public int setDownResources() {
        return 0;
    }

    public void setSelectTextColor(@ColorRes int resId) {
        selectColor = resId;
    }

    public void setUnSelectTextColor(@ColorRes int resId) {
        unSelectColor = resId;
    }

    public interface OnTextStyle { void onItem(CityHolder item); }
    private OnTextStyle mOnTextStyle;
    public void setItemTextStyle(OnTextStyle mOnTextStyle) {
        this.mOnTextStyle = mOnTextStyle;
    }
}
